package cn.gson.springboot.model.service;

import java.util.Date; 
import java.util.List;

import javax.validation.constraints.Size;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.ApplyDAO;
import cn.gson.springboot.model.dao.ApplyDetailDAO;
import cn.gson.springboot.model.dao.DrugsYkDao;
import cn.gson.springboot.model.dao.InventoryYkDAO;
import cn.gson.springboot.model.dao.OutboundYkDAO;
import cn.gson.springboot.model.dao.OutboundYkxqDAO;
import cn.gson.springboot.model.mapper.OutboundYkMapper;
import cn.gson.springboot.model.mapper.OutboundYkxqMapper;
import cn.gson.springboot.model.mapper.ReturnDrugsYkxqMapper;
import cn.gson.springboot.model.pojos.Apply;
import cn.gson.springboot.model.pojos.Applydetai;
import cn.gson.springboot.model.pojos.Ck;
import cn.gson.springboot.model.pojos.DestructionDetailsYk;
import cn.gson.springboot.model.pojos.DestructionYk;
import cn.gson.springboot.model.pojos.DrugsYk;
import cn.gson.springboot.model.pojos.InventoryYk;
import cn.gson.springboot.model.pojos.OutboundDetailsYk;
import cn.gson.springboot.model.pojos.OutboundYk;
import cn.gson.springboot.model.pojos.ReturnDrugsYk;
import cn.gson.springboot.model.vo.ApplyVO;
import cn.gson.springboot.model.vo.DestructionYkVO;
import cn.gson.springboot.model.vo.OutboundYkVO;

@Service
@Transactional(rollbackFor=Exception.class)
public class OutboundYkService {
	@Autowired
	private OutboundYkMapper obm;
	@Autowired
	private OutboundYkDAO obb;
	@Autowired
	private OutboundYkxqDAO obxqd;
	@Autowired
	private OutboundYkxqMapper obxqm;
	@Autowired
	private DrugsYkDao hdao;
	@Autowired
	private ApplyDAO ad;
	@Autowired
	private ApplyDetailDAO apdd;
	@Autowired
	private InventoryYkDAO itd;
	
	//查询所有
	public JSONArray findchuku(Integer outbound_id){
		return obm.findchuku(outbound_id);
	}
	
	//新增
	public OutboundYk adds(OutboundYk d) {
		return obb.save(d);
	}
	
	//修改状态
	public OutboundYk idckxgzt(Integer outboundId) {
		return obb.xiugaickzt(outboundId);
	}
	
	//查看详情
	public JSONArray findbycyfxq(Integer outbound_id){
		return obxqm.findcyfxq(outbound_id);
	}
	
	//请领单详情
	public JSONArray qldxiangqing(Integer apply_id) {
		return obm.qldxiangqing(apply_id);
	}
	
	
	//生成出库订单
			public void saveOutbound(OutboundYkVO OutboundYkVO) {
				System.out.println("我到了这里");
				//获取请领单进行新增
				OutboundYk out = OutboundYkVO.getOutbound();
				out.setOutboundDate(new Date());//时间
				out.setApply(OutboundYkVO.getOutbound().getApply());
				OutboundYk save = obb.save(out);
				
				List<OutboundDetailsYk> outbo = OutboundYkVO.getOdetails();
				//循环
				for (OutboundDetailsYk pur : outbo) {
					OutboundDetailsYk ok=new OutboundDetailsYk();
					//添加订单信息
					ok.setOutboundYk(out);
					
					DrugsYk dk = hdao.findById(pur.getDrugsYk().getDrugId()).get();
					ok.setDrugsYk(dk);
					//根据页面传过来的vo类-----得到药品单个发药数量
					
					//根据药品id找到所有药品批次
					JSONArray selectPici= obm.findchukupici(pur.getDrugsYk().getDrugId());
					
					if(selectPici.size()>0) {
						Integer xuyao = pur.getOutdetailsShouldNumber();
				    //根据药品id与药品批次找到药品库存数量
						for (int i = 0; i <selectPici.size(); i++) {
					    Integer pici = selectPici.getJSONObject(i).getInteger("drug_pc");
				    	System.out.println("批次是"+pici);
						//查询药品
						InventoryYk inventory = itd.cxkc(pur.getDrugsYk().getDrugId(),pici);
						//重新赋值一下药品对象
						ok.setInventoryYk(inventory);
				    	//库存数量
						int kucun=inventory.getInventoryNumber();
						System.out.println("库存数量是："+kucun);
						//批次
						ok.setDrugPc(pici);
						if(kucun-xuyao>0) {
							inventory.setInventoryNumber(kucun-xuyao);
							ok.setOutdetailsShouldNumber(xuyao);
						}else {
							inventory.setInventoryNumber(kucun-kucun);
							xuyao=kucun-xuyao;
							xuyao=Math.abs(xuyao);
							ok.setOutdetailsShouldNumber(kucun);
						}
						if(kucun-Math.abs(xuyao)>0) {
							break;
						}
				    }
				    obxqd.save(ok);
					}										
				}
				
			}

}
